<?php // no direct access
defined('_JEXEC') or die('Restricted access');

  $myEmail = $_GET['u'];

  $myprecious = -1;

  $myEmailLabel = $this->myEmailLabel;
  $recipient = $this->recipient;

  $notifyUnsub = $this->notifyUnsub;
  $subject = $this->subject;
  $fromName = $this->fromName;
  $fromEmail = $this->fromEmail;
  $fromNameConfirmun = $this->fromNameConfirmun;
  $fromEmailConfirmun = $this->fromEmailConfirmun;
  $subjectConfirmun = $this->subjectConfirmun;
  $sendingWithSetEmail = $this->sendingWithSetEmail;

  $uredirectToPage = $this->uredirectToPage;
  $uredirectPage = $this->uredirectToPage;

  $myRealEmail = '';

  for ($i=0, $n=count( $this->subs ); $i < $n; $i++) {
    $row = &$this->subs[$i];

    if (md5($row[2]) == $myEmail) {
      $myprecious = $row[0];
      $myRealEmail = $row[2];
      break;
    }
  }

  if ($myprecious != -1) {
    $db =& JFactory::getDBO();
    $query = 'DELETE FROM `#__nspro_subs` WHERE `id` = "'.mysql_escape_string($myprecious).'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Deletion Error. Please try again');
      return;
    }
    else {

      if ($notifyUnsub) {
        $myMessage = $myEmailLabel . ' ' . $myRealEmail . ', ' . date("r");
        $mailSender = &JFactory::getMailer();
        $mailSender->addRecipient($recipient);

        if ($sendingWithSetEmail) {
          $mailSender->setSender(array($fromEmail,$fromName));
        }
        else {

          $query = 'SELECT * FROM `#__nspro_subs` WHERE `email` = "'.mysql_escape_string($email).'"';
          $db->setQuery($query);
          $myResult = $db->loadObject();

          $mailSender->setSender(array($myRealEmail,$myResult->name));
        }

        $mailSender->setSubject($subject);
        $mailSender->setBody($myMessage);

        if (!$mailSender->Send()) {
          $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was not completed') . '</span>';
          print $myReplacement;
        }
        else {
          $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was completed') . '</span>';
          if (!$uredirectToPage) {
            print $myReplacement;
          }
          else {
            header("Location: ".$uredirectPage);
          }
        }
      }
      else {
        $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was completed') . '</span>';
        if (!$uredirectToPage) {
          print $myReplacement;
        }
        else {
          header("Location: ".$uredirectPage);
        }
      }

      $query = 'DELETE FROM `#__nspro_subs` WHERE `email` = "'.mysql_escape_string($myRealEmail).'"';
      $db->setQuery($query);
      if (!$db->query()) {
        print JText::_('Database Deletion Error. Please try again');
        return;
      }
    }
  }
  else {
    print JText::_('This link has expired');
  }


?>
